import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.lang.Math;
public class Calculator{
    public static JTextJFrame frame;
    public static void main(String[] arg){
        frame=new JTextJFrame();
        frame.setLocation(500,200);
        frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        frame.addWindowListener(new closeing());
        frame.setVisible(true);
        
    }
}
class JTextJFrame extends JFrame{
    private int counter=1;
    private double R1,R2;
    private boolean Sqrt=true;
    private JTextField Txet;
    private JLabel label;
    public JTextJFrame (){
        Txet=new JTextField("0",25);
        Txet.setEditable(false);
        Txet.setHorizontalAlignment(JTextField.RIGHT);
        label=new JLabel(" ",JLabel.LEADING);
        JLabel label1=new JLabel(new ImageIcon("image\\zhu.gif"));
        setTitle("java计算器");
        setSize(360,320);
        Icon[] JPG={new ImageIcon("image\\a0.jpg"),new ImageIcon("image\\a1.jpg"),new ImageIcon("image\\a2.jpg"),new ImageIcon("image\\a3.jpg"),new ImageIcon("image\\a4.jpg"),new ImageIcon("image\\a5.jpg"),new ImageIcon("image\\a6.jpg"),new ImageIcon("image\\a7.jpg"),new ImageIcon("image\\a8.jpg"),new ImageIcon("image\\a9.jpg")};
        Icon[] JPGB={new ImageIcon("image\\ab0.jpg"),new ImageIcon("image\\ab1.jpg"),new ImageIcon("image\\ab2.jpg"),new ImageIcon("image\\ab3.jpg"),new ImageIcon("image\\ab4.jpg"),new ImageIcon("image\\ab5.jpg"),new ImageIcon("image\\ab6.jpg"),new ImageIcon("image\\ab7.jpg"),new ImageIcon("image\\ab8.jpg"),new ImageIcon("image\\ab9.jpg")};
        MouseString[] MString={new MouseString("0"),new MouseString("1"),new MouseString("2"),new MouseString("3"),new MouseString("4"),new MouseString("5"),new MouseString("6"),new MouseString("7"),new MouseString("8"),new MouseString("9")};
        MouseString ZFMstring=new MouseString("+-");
        MouseString MstringD=new MouseString(".");
        CCEString CCEC=new CCEString("C");
        CCEString CCECE=new CCEString("CE");
        Operation LAddplus=new Operation('+');
        Operation LSubtract=new Operation('-');
        Operation LMultiply=new Operation('*');
        Operation LEquals=new Operation('=');
        Operation LDivide=new Operation('/');
        Operation LSqrt=new Operation('S');
        
        Container contentPane=getContentPane();
                     //侦听器插入行
        JSplitPane split1,split2;
        JPanel LTPanel=new JPanel();
        JPanel LTPanel1=new JPanel();
        LTPanel1.add(label1);
        LTPanel1.add(Txet);
        LTPanel.setLayout(new GridLayout(2,1,0,0));
        LTPanel.add(label);
        LTPanel.add(LTPanel1);
        JPanel buttonPanel1=new JPanel();
        buttonPanel1.setCursor(new Cursor(Cursor.HAND_CURSOR)); 
        buttonPanel1.setLayout(new GridLayout(4,3,4,3));//试加布局管理器
        JPanel buttonPanel2=new JPanel();
        buttonPanel2.setLayout(new GridLayout(4,2,4,3));
        JButton button1=new JButton();//按钮
        button1.setIcon(JPG[1]);
        button1.setPressedIcon(JPGB[1]);
        button1.addMouseListener(MString[1]);
        buttonPanel1.add(button1);
        JButton button2=new JButton();
        button2.setIcon(JPG[2]);
        button2.setPressedIcon(JPGB[2]);
        button2.addMouseListener(MString[2]);
        buttonPanel1.add(button2);
        JButton button3=new JButton();
        button3.setIcon(JPG[3]);
        button3.setPressedIcon(JPGB[3]);
        button3.addMouseListener(MString[3]);
        buttonPanel1.add(button3);
        JButton button4=new JButton();
        button4.setIcon(JPG[4]);
        button4.setPressedIcon(JPGB[4]);
        button4.addMouseListener(MString[4]);
        buttonPanel1.add(button4);
        JButton button5=new JButton();
        button5.setIcon(JPG[5]);
        button5.setPressedIcon(JPGB[5]);
        button5.addMouseListener(MString[5]);
        buttonPanel1.add(button5);
        JButton button6=new JButton();
        button6.setIcon(JPG[6]);
        button6.setPressedIcon(JPGB[6]);
        button6.addMouseListener(MString[6]);
        buttonPanel1.add(button6);
        JButton button7=new JButton();
        button7.setIcon(JPG[7]);
        button7.setPressedIcon(JPGB[7]);
        button7.addMouseListener(MString[7]);
        buttonPanel1.add(button7);
        JButton button8=new JButton();
        button8.setIcon(JPG[8]);
        button8.setPressedIcon(JPGB[8]);
        button8.addMouseListener(MString[8]);
        buttonPanel1.add(button8);
        JButton button9=new JButton();
        button9.setIcon(JPG[9]);
        button9.setPressedIcon(JPGB[9]);
        button9.addMouseListener(MString[9]);
        buttonPanel1.add(button9);
        JButton buttonD=new JButton(".");
        buttonD.addMouseListener(MstringD);
        buttonPanel1.add(buttonD);
        JButton button0=new JButton();
        button0.setIcon(JPG[0]);
        button0.setPressedIcon(JPGB[0]);
        button0.addMouseListener(MString[0]);
        buttonPanel1.add(button0);
        JButton buttonZF=new JButton("-/+");
        buttonZF.addMouseListener(ZFMstring);
        buttonPanel1.add(buttonZF);//buttonPanel1结束
        JButton buttonC=new JButton("C");
        buttonC.addMouseListener(CCEC);
        buttonPanel2.add(buttonC);
        JButton buttonCE=new JButton("CE");
        buttonCE.addMouseListener(CCECE);
        buttonPanel2.add(buttonCE);
        JButton buttonDivide=new JButton("/");
        buttonDivide.addMouseListener(LDivide);
        buttonPanel2.add(buttonDivide);
        JButton buttonMultiply=new JButton("*");
        buttonMultiply.addMouseListener(LMultiply);
        buttonPanel2.add(buttonMultiply);
        JButton buttonAddplus=new JButton("+");
        buttonAddplus.addMouseListener(LAddplus);
        buttonPanel2.add(buttonAddplus);
        JButton buttonSubtract=new JButton("-");
        buttonSubtract.addMouseListener(LSubtract);
        buttonPanel2.add(buttonSubtract);
        JButton buttonSqrt=new JButton("sqrt");
        buttonSqrt.addMouseListener(LSqrt);
        buttonPanel2.add(buttonSqrt);
        JButton buttonEquals=new JButton("=");
        buttonEquals.addMouseListener(LEquals);
        buttonPanel2.add(buttonEquals);//buttonPanel2结束
        split1=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,buttonPanel1,buttonPanel2);
        split1.setDividerLocation(200);
        split2=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,LTPanel,split1);
        contentPane.add(split2,BorderLayout.CENTER);
    }
    public void JLabelEquals(){
         label.setText(label.getText()+"="+Txet.getText());
    }
    public void JLabelString(String S){
         label.setText(label.getText()+S);
    }
    public void addString(String S){
         if(counter>=9){
             if(S==".")
                 Txet.setText("0.");
             else
                 Txet.setText(S);
             counter=counter-8;
             return;
         }
         if(Sqrt==false){
             if(S==".")
                 Txet.setText("0.");
             else
                 Txet.setText(S);
             Sqrt=true;
             return;
         }
         if(Txet.getText().length()==20)
             return;
         if((Txet.getText().equals("0")||Txet.getText().equals("-0"))&&!S.equals(".")&&!S.equals("+-"))
             Txet.setText(S);
         else{
             if(S.equals("+-")){
             if(Txet.getText().charAt(0)=='-')
                 Txet.setText(Txet.getText().substring(1));
             else
                 Txet.setText("-"+Txet.getText());
             }
             else
                 if(-1!=Txet.getText().indexOf(".")&&S.equals("."))
                     ;
                 else
                     Txet.setText(Txet.getText()+S);
              
        }
    }
    public void CCEString(String S){
     if(S=="C"&&Txet.getText().length()!=1){
         Txet.setText(new String((Txet.getText().toCharArray()),0,(Txet.getText().length()-1))); 
         label.setText(new String((label.getText().toCharArray()),0,(label.getText().length()-1)));
     }
     else{
         Txet.setText("0");
         if(Txet.getText()!="0");
         label.setText((new String((label.getText().toCharArray()),0,(label.getText().length()-1)))+" ");
         }
     if(S=="CE"){
         R1=0;
         R2=0;
         counter=1;
         label.setText(" ");
     }
    }
    public void Addplus(){//加方法
        switch (counter){
             case 1://加
                 R1=R1+(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
             case 2://减
                 R1=R1-(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
             case 3://乘
                 R1=R1*(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
             case 4://除
                 R1=R1/(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
             case 5://加乘
                 R1=R2+R1*(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
             case 6://加除
                 R1=R2+R1/(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
             case 7://减乘
                 R1=R2-R1*(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
             case 8://减除
                 R1=R2-R1/(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
        }
        counter=9;//1+8
    }
    public void Subtract(){//减方法
        switch (counter){
             case 1://加
                 R1=R1+(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
             case 2://减
                 R1=R1-(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
             case 3://乘
                 R1=R1*(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
             case 4://除
                 R1=R1/(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
             case 5://加乘
                 R1=R2+R1*(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
             case 6://加除
                 R1=R2+R1/(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
             case 7://减乘
                 R1=R2-R1*(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
             case 8://减除
                 R1=R2-R1/(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 break;
        }
        counter=10;//2+8
    }
    public void Multiply(){//乘方法
        switch (counter){
             case 1://加
                 R2=R1 ;
                 R1=Double.parseDouble(Txet.getText());
                 counter=13;//5+8
                 break;
             case 2://减
                 R2=R1 ;
                 R1=Double.parseDouble(Txet.getText());
                 counter=15;//7+8
                 break;
             case 3://乘
                 R1=R1*(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 counter=11;//3+8
                 break;
             case 4://除
                 R1=R1/(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 counter=11;//3+8
                 break;
             case 5://加乘
                 R1=R1*(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 counter=13;//5+8
                 break;
             case 6://加除
                 R1=R1/(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 counter=13;//5+8
                 break;
             case 7://减乘
                 R1=R1*(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 counter=15;//7+8
                 break;
             case 8://减除
                 R1=R1/(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 counter=15;//7+8
                 break;
             default:
                 counter=11;
        }
        
    }
    public void Divide(){//除方法
        switch (counter){
             case 1://加
                 R2=R1 ;
                 R1=Double.parseDouble(Txet.getText());
                 counter=14;//6+8
                 break;
             case 2://减
                 R2=R1 ;
                 R1=Double.parseDouble(Txet.getText());
                 counter=16;//8+8
                 break;
             case 3://乘
                 R1=R1*(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 counter=12;//4+8
                 break;
             case 4://除
                 R1=R1/(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 counter=12;//4+8
                 break;
             case 5://加乘
                 R1=R1*(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 counter=14;//6+8
                 break;
             case 6://加除
                 R1=R1/(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 counter=14;//6+8
                 break;
             case 7://减乘
                 R1=R1*(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 counter=16;//8+8
                 break;
             case 8://减除
                 R1=R1/(Double.parseDouble(Txet.getText())) ;
                 Txet.setText(Double.toString(R1));
                 counter=16;//8+8
                 break;
             default:
                 counter=12;
        }
        
    }
    public void Equals(){//等于方法
        switch (counter){
             case 1://加
                 R1=R1+Double.parseDouble(Txet.getText());
                 break;
             case 2://减
                 R1=R1-Double.parseDouble(Txet.getText());
                 break;
             case 3://乘
                 R1=R1*(Double.parseDouble(Txet.getText())) ;
                 break;
             case 4://除
                 R1=R1/(Double.parseDouble(Txet.getText())) ;
                 break;
             case 5://加乘
                 R1=R2+R1*(Double.parseDouble(Txet.getText())) ;
                 break;
             case 6://加除
                 R1=R2+R1/(Double.parseDouble(Txet.getText())) ;
                 break;
             case 7://减乘
                 R1=R2-R1*(Double.parseDouble(Txet.getText())) ;
                 break;
             case 8://减除
                 R1=R2-R1/(Double.parseDouble(Txet.getText())) ;
                 break;
        }
        Txet.setText(Double.toString(R1));
        counter=9;//1+8
    }
    public void Sqrt(){//正平方根
        Txet.setText(Double.toString(Math.sqrt(Double.parseDouble(Txet.getText()))));
        label.setText(label.getText()+"Sqrt:"+Txet.getText());
        Sqrt=false;
    }
}
class Operation extends MouseAdapter{
     char C;
     public Operation(char C){
         this.C=C;
     }
     public void mousePressed(MouseEvent e){
         switch (C){
             case '+':
                 Calculator.frame.Addplus();Calculator.frame.JLabelString("+");break;
             case '-':
                 Calculator.frame.Subtract();Calculator.frame.JLabelString("-");break;
             case '*':
                 Calculator.frame.Multiply();Calculator.frame.JLabelString("*");break;
             case '/':
                 Calculator.frame.Divide();Calculator.frame.JLabelString("/");break;
             case 'S':
                 Calculator.frame.Sqrt();break;
             case '=':
                 Calculator.frame.Equals();Calculator.frame.JLabelEquals();break;
        }
    }
}
class CCEString extends MouseAdapter{
    String S;
    public CCEString(String S){
         this.S=S;
    }
    public void mousePressed(MouseEvent e){
        Calculator.frame.CCEString(S);
    }
}
class MouseString extends MouseAdapter{
     String S;
     public MouseString(String S){
         this.S=S;
    }
    public void mousePressed(MouseEvent e){
        Calculator.frame.addString(S);
        Calculator.frame.JLabelString(S);
    }
}
class closeing extends WindowAdapter{
     public void windowClosing(WindowEvent e){
     System.out.print("再见"+"\n"+"归属黄国梁项目小组");
     System.exit(0);
    }
}